package com.houger.ddd.seckill.infra.datamanager;

import com.houger.ddd.datamanager.manager.aop.DataCache;
import com.houger.ddd.datamanager.manager.aop.DataCacheEvict;
import com.houger.ddd.datamanager.utils.CacheType;
import com.houger.ddd.seckill.application.MapperUtils;
import com.houger.ddd.seckill.template.context.ActivityContext;
import com.houger.ddd.seckill.infra.entity.ActivityRewardItem;
import com.houger.ddd.seckill.infra.entity.ActivityTemplateBasic;
import com.houger.ddd.seckill.infra.entity.ActivityTemplateBizConfig;
import com.houger.ddd.seckill.infra.entity.ActivityTemplateUiConfig;
import com.houger.ddd.seckill.template.ActivityTemplate;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class ActivityContextDataManager {

    @Resource
    ActivityTemplateBasicDataManager basicDataManager;
    @Resource
    ActivityTemplateBizConfigDataManager bizConfigDataManager;
    @Resource
    ActivityRewardItemDataManager rewardItemDataManager;
    @Resource
    ActivityTemplateUiConfigDataManager uiConfigDataManager;

    @DataCache(type = {CacheType.LOCAL,CacheType.REDIS}, group = "context",key = "('templateCode=').concat(#templateCode)",
        localExpire = 1 * 60,
        redisExpire = 1 * 60)
    public ActivityContext queryContextByTemplateCode(ActivityTemplate template, String templateCode) {
        ActivityTemplateBasic basic = basicDataManager.queryBasicByCode(templateCode);
        ActivityTemplateBizConfig bizConfig = bizConfigDataManager.queryBizByTemplateCode(templateCode);
        ActivityTemplateUiConfig uiConfig = uiConfigDataManager.queryUiByTemplateId(basic.getId());
        List<ActivityRewardItem> itemList = rewardItemDataManager.queryItemListByTemplateId(basic.getId());

        return ActivityContext.builder()
                .basicDto(MapperUtils.getBasicDto(basic))
                .bizConfigDto(MapperUtils.getBizConfigDto(bizConfig, template.getBizExtDataObjClass()))
                .uiConfigDto(MapperUtils.getUiConfigDto(uiConfig, template.getUiExtDataObjClass()))
                .rewardItemDtoList(MapperUtils.getRewardItemDtoList(itemList, template.getRewardItemExtDataObjClass()))
                .build();
    }

    @DataCacheEvict(group = "context",key = {
            "('templateCode=').concat(#templateCode)"},
            type = {CacheType.LOCAL,CacheType.REDIS}
    )
    public void evict(String templateCode){

    }

}
